﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Runtime.Intrinsics;
using System.Text;
using System.Threading.Tasks;

namespace SimdTest2.Methods
{
    [Algorithm(true, AlgorithmType.Four)]
    public unsafe class FourMethod : AlgorithmBase
    {
        public FourMethod(int row, int col) : base(row, col)
        {
        }
        private uint[,] _data;
        protected override void CreateData()
        {
            _data = new uint[_row, _column];
            Random random = new Random();
            for (int i = 0; i < _row; i++)
            {
                for (int j = 0; j < _column; j++)
                {
                    _data[i,j] = (uint)random.Next(999);
                }
            }
        }
        const uint a = 5;
        const uint b = a << 8;
        protected override void OrdinaryMethod()
        {
            for (int i = 0; i < _row; i++)
            {
                for (int j = 0; j < _column; j++)
                {
                    uint y = _data[i, j] * a + b;
                }
            }
        }

        protected override void SimdMethod()
        {
            fixed (uint* data = _data)
            {
                int vsize = Vector128<int>.Count;
                Vector128<uint> aVector128 = Vector128.Create(a);
                Vector128<uint> bVector128 = Vector128.Create(b);
                for (int i = 0; i < _row; i++)
                {
                    int curi = i * _column;
                    for (int j = 0; j < _column; j+= vsize)
                    {
                        Vector128<uint> tempV = *(Vector128<uint>*)((data + curi + j)) * aVector128;
                        Vector128<uint> yVector256 = Sse2.Add(tempV, bVector128);
                    }
                }
            }
        }
    }
}
